<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//======================================================================================================================
//================================================  INITIALIZATORS   ===================================================
//======================================================================================================================

   protected function _initDbAdapter()
   {
      $db=$this->  getDbAdapter();
      Zend_Db_Table::setDefaultAdapter($db);
   }

   protected function _initAcl()
   {
      $acl_builder=new Application_Model_AclBuilder();
      $auth=Zend_Auth::getInstance();

      $acl=$acl_builder->  createACL();

      Zend_Registry::set('acl',$acl);
      Zend_Registry::set('auth',$auth);
   }

   protected function _initPlugins()
   {
      $this->privPlugin();
      $this->menuPlugin();
   }

   protected function _initLayoutEnv()
   {
      $view=$this->getViewLayout();

      //sets doctype ...
      $view->doctype('HTML4_STRICT');

      //sets global title
      $view->headTitle('Weblux - Linux admin panel')
           ->setSeparator(' : ');

      //sets global CSS stylesheets - including stylesheets from Blueprint CSS framework
      $view->headLink()->appendStylesheet('/css/blueprint/screen.css', 'screen, projection')
                       ->appendStylesheet('/css/blueprint/print.css', 'print')
                       ->appendStylesheet('/css/blueprint/ie.css', 'screen, projection', "IE")
                       ->appendStylesheet('/css/custom/custom.css');

      //set meta tags
      $view->headMeta()->appendHttpEquiv('Content-Type','text/html; charset=UTF-8')
                       ->appendHttpEquiv('Content-Language', 'en-US')
                       ->appendHttpEquiv('pragma', 'no-cache')
                       ->appendHttpEquiv('Cache-Control', 'no-cache');

   }

   protected function _initMenus()
   {
      $view=$this->  getViewLayout();

      $menus=new Application_Model_MenuMapper();
      $topmenu= $menus-> getMenu('top_menu');
      $leftmenu= $menus-> getMenu('left_menu');
      $acl=Zend_Registry::get('acl');
      $auth=Zend_Registry::get('auth');
      $identity=$auth->getStorage()->read();
      if($identity==null)
      {
         $role='guest';
      }
      else
      {
         $role=$identity->role;
      }
      $view->navigation($leftmenu)->setAcl($acl)->setRole($role);
      $view->navigation($topmenu)-> setAcl($acl)->setRole($role);


      Zend_Registry::set('top_menu', $topmenu);
      Zend_Registry::set('left_menu', $leftmenu);
      Zend_Registry::set('right_menu','');
   }

   protected function _initShell()
   {
      Zend_Registry::set('shell_path',APPLICATION_PATH.'/shell');
   }

//======================================================================================================================
//====================================================  PLUGINS   ======================================================
//======================================================================================================================

   private function menuPlugin()
   {
      $front=$this->  getFrontController();

      $front->registerPlugin(new Application_Plugin_ModuleMenuLoader());
   }

   private function privPlugin()
   {
      $front=$this->  getFrontController();

      $acl=Zend_Registry::get('acl');
      $auth=Zend_Registry::get('auth');
      $front->registerPlugin(new Application_Plugin_PrivCheck($acl,$auth));
   }


//======================================================================================================================
//====================================================  HELPERS   ======================================================
//======================================================================================================================


   public function getFrontController()
   {

      if(!$this->hasResource('frontController'))
      {
         $this->bootstrap('frontController');
      }
      $front = $this->getResource('frontController');

      return $front;
   }

   public function getViewLayout()
   {
      if(!$this->hasResource('layout'))
      {
         $this->bootstrap('layout');
      }

      $layout=$this->getResource('layout');
      $view=$layout->getView();
      return $view;
   }

   public function getDbAdapter()
   {
      if(!$this->hasResource('db'))
      {
         $this->bootstrap('db');
      }

      $db=$this->getResource('db');
      return $db;
   }
}